What is @ethersproject/logger?
@ethersproject/logger is a logging utility designed for use with the ethers.js library. It provides a simple and consistent way to log messages, warnings, and errors, making it easier to debug and track issues in your Ethereum-related applications.
What are @ethersproject/logger's main functionalities?
Logging Messages
This feature allows you to log informational messages. The logger instance is created with a context name ('my-app' in this case), which helps in identifying the source of the log messages.
const { Logger } = require('@ethersproject/logger');
const logger = new Logger('my-app');
logger.info('This is an info message');
Logging Warnings
This feature allows you to log warning messages. Warnings are useful for indicating potential issues that are not necessarily errors but should be looked into.
const { Logger } = require('@ethersproject/logger');
const logger = new Logger('my-app');
logger.warn('This is a warning message');
Logging Errors
This feature allows you to log error messages. Errors indicate serious issues that need immediate attention.
const { Logger } = require('@ethersproject/logger');
const logger = new Logger('my-app');
logger.error('This is an error message');
Custom Error Codes
This feature allows you to create and throw custom errors with specific error codes. This is useful for categorizing and handling different types of errors in a more granular way.
const { Logger } = require('@ethersproject/logger');
const logger = new Logger('my-app');
const error = logger.makeError('Custom error message', 'CUSTOM_ERROR_CODE');
logger.throwError('This is a custom error', 'CUSTOM_ERROR_CODE', { additional: 'info' });
Other packages similar to @ethersproject/logger
winston
Winston is a versatile logging library for Node.js that supports multiple transports (e.g., console, file, HTTP). It is more feature-rich compared to @ethersproject/logger, offering advanced logging capabilities like log levels, custom formats, and more.
bunyan
Bunyan is another logging library for Node.js that focuses on JSON logging. It provides a simple API for logging and supports features like log levels, serializers, and streams. Bunyan is more suited for applications that require structured logging.
pino
Pino is a fast and low-overhead logging library for Node.js. It is designed for high-performance logging and supports features like log levels, serializers, and transports. Pino is ideal for applications where performance is a critical concern.
Logger
This sub-module is part of the ethers project.
It is responsible for managing logging and errors to maintain a standard
structure.
For more information, see the documentation.
Importing
Most users will prefer to use the umbrella package,
but for those with more specific needs, individual components can be imported.
const {
Logger,
ErrorCode,
LogLevel,
} = require("@ethersproject/logger");
License
MIT License.